package com.shop.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Component
public class UploadUtils {
    public String add(MultipartFile file) throws Exception {
        String path = null;
        if (file != null) {
            String type = null;
            String fileName = file.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    System.out.println("文件名称："+fileName);
                    path = "C:\\Users\\dell\\Desktop\\graduation-project\\shop_backend\\src\\main\\resources\\static\\images\\";
                    String pathName = path + fileName;
                    System.out.println("存放图片文件的路径:" + pathName);
                    file.transferTo(new File(pathName));
                    System.out.println("文件成功上传到指定目录下");
                    return fileName + "." + type;
                }
            } else {
                return "不是我们想要的文件类型,请按要求重新上传";
            }
        } else {
            return "文件类型为空";
        }
        return "已经成功上传到指定目录";
    }
}